<template>
	<div v-if="indexShow">
		<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
			<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="名称关键词" name="searchKey">
							<a-input v-model:value="searchFormState.searchKey" placeholder="请输入名称关键词" />
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="分类" name="category">
							<a-select v-model:value="searchFormState.category" placeholder="请选择分类" :options="categoryOptions" />
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-space>
							<a-button type="primary" @click="tableRef.refresh()">
								<template #icon><SearchOutlined /></template>
								查询
							</a-button>
							<a-button @click="reset">
								<template #icon><redo-outlined /></template>
								重置
							</a-button>
						</a-space>
					</a-col>
				</a-row>
			</a-form>
		</a-card>
		<a-card :bordered="false">
			<s-table
				ref="tableRef"
				:columns="columns"
				:data="loadData"
				:alert="options.alert.show"
				bordered
				:row-key="(record) => record.id"
				:tool-config="toolConfig"
				:row-selection="options.rowSelection"
			>
				<template #operator class="table-operator">
					<a-space>
						<a-button type="primary" @click="formRef.onOpen()">
							<template #icon>
								<plus-outlined />
							</template>
							<span>新增</span>
						</a-button>
						<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchTemplatePrint" />
					</a-space>
				</template>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'type'">
						<a-tag v-if="record.type === 'DESIGN'" color="cyan">
							{{ $TOOL.dictTypeData('TEMPLATE_PRINT_TYPE', record.type) }}
						</a-tag>
						<a-tag v-if="record.type === 'DEFINE'" color="blue">
							{{ $TOOL.dictTypeData('TEMPLATE_PRINT_TYPE', record.type) }}
						</a-tag>
					</template>
					<template v-if="column.dataIndex === 'category'">
						{{ $TOOL.dictTypeData('TEMPLATE_PRINT_CATEGORY', record.category) }}
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space>
							<a @click="openDesign(record)" v-if="record.type === 'DESIGN'">设计</a>
							<a @click="previewRef.onOpen(record)" v-if="record.type === 'DEFINE'">预览</a>
							<a-divider type="vertical" />
							<a @click="formRef.onOpen(record)">编辑</a>
							<a-divider type="vertical" />
							<a-dropdown>
								<a class="ant-dropdown-link">
									更多
									<DownOutlined />
								</a>
								<template #overlay>
									<a-menu>
										<a-menu-item>
											<a @click="handleCopy(record)">
												<a-button type="link" size="small">复制</a-button>
											</a>
										</a-menu-item>
										<a-menu-item>
											<a-popconfirm
												title="确定要删除此打印模板吗？"
												placement="topLeft"
												@confirm="deleteFlwTemplatePrint(record)"
											>
												<a-button type="link" danger size="small">删除</a-button>
											</a-popconfirm>
										</a-menu-item>
									</a-menu>
								</template>
							</a-dropdown>
						</a-space>
					</template>
				</template>
			</s-table>
		</a-card>
		<Form ref="formRef" @successful="tableRef.refresh()" />
	</div>
	<print-design v-else ref="printDesignRef" @back="goBack" />
	<preview ref="previewRef" />
</template>

<script setup name="flwTemplatePrint">
	import templatePrintApi from '@/api/flw/templatePrintApi'
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import { cloneDeep } from 'lodash-es'
	import PrintDesign from './printDesign.vue'
	import Preview from './preview.vue'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const printDesignRef = ref()
	const indexShow = ref(true)
	const previewRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '模板名称',
			dataIndex: 'name'
		},
		{
			title: '分类',
			dataIndex: 'category'
		},
		{
			title: '模板类型',
			dataIndex: 'type'
		},
		{
			title: '排序',
			dataIndex: 'sortCode',
			sorter: true
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			sorter: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '220px'
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		return templatePrintApi.templatePrintPage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 复制
	const handleCopy = (record) => {
		const param = cloneDeep(record)
		delete param.id
		param.title = param.title + '_副本'
		// TODO：调用复制接口
		tableRef.value.refresh(true)
	}
	// 设计
	const openDesign = (record) => {
		indexShow.value = false
		nextTick(() => {
			printDesignRef.value.onOpen(record)
		})
	}
	// 返回
	const goBack = () => {
		indexShow.value = true
		nextTick(() => {
			tableRef.value.refresh()
		})
	}
	// 删除
	const deleteFlwTemplatePrint = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		templatePrintApi.templatePrintDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchTemplatePrint = (params) => {
		templatePrintApi.templatePrintDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	// 分类
	const categoryOptions = tool.dictList('TEMPLATE_PRINT_CATEGORY')
</script>
